import java.awt.Graphics;

import me.w70.bot.Bot;
import me.w70.bot.scripting.Random;
import me.w70.bot.wrappers.RSInterface;
import me.w70.bot.wrappers.RSNPC;
import me.w70.hooks.Tile;



public class Mime extends Random {

	int animation;

	public Tile MimeTile = new Tile(2008, 4762);

	int status = 0;

	@Override
	public boolean canRun() {

		if (!isLoggedIn()) {
			return false;
		}

		if (distanceTo(MimeTile) <= 15) {
			return true;
		} else {
			return false;
		}

	}

	public boolean ClickAnimation(final String find) {
		for (int a = 0; a < MimeInterface().getChildren().length; a++) {
			final RSInterface child = getInterface(188, a);
			if (child.getText().contains(find)) {
				return atInterface(child);
			}
		}
		return false;
	}

	@Override
	public String getScriptAuthor() {
		return "spooky";
	}

	@Override
	public String getScriptName() {
		return "Mime";
	}



	public void listen(final int timer) {
		if (MimeInterface().isValid()) {
			return;
		}
		final long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start >= timer) {
			if (MimeInterface().isValid()) {
				wait(random(800, 1200));
				return;
			}
		}
	}

	@Override
	public int loop() {
		final RSNPC n = getNearestNPCByName("Mime");
		if (n == null || !(distanceTo(MimeTile) <= 15)) {
			Bot.addMessage(this, "Mime not found!");
			return -1;
		}

		if (getInterface(372, 2).getText().contains("Watch")) {
			if (n.getAnimation() != -1 && n.getAnimation() != 858) {
				animation = n.getAnimation();
				Bot.addMessage(this, "New Mime Animation: " + animation);
				wait(1000);
				listen(5000);
				return random(300, 600);
			}
			return random(400, 600);
		}

		if (MimeInterface().isValid()) {
			Bot.addMessage(this,"Clicking Text According to Animation: " + animation);
			if (animation != -1) {
				switch (animation) {
				case 857:
					ClickAnimation("Think");
					break;
				case 860:
					ClickAnimation("Cry");
					break;
				case 861:
					ClickAnimation("Laugh");
					break;
				case 866:
					ClickAnimation("Dance");
					break;
				case 1128:
					ClickAnimation("Wall");
					break;
				case 1129:
					ClickAnimation("Lean");
					break;
				case 1130:
					ClickAnimation("Rope");
					break;
				case 1131:
					ClickAnimation("Box");
					break;
				default:
					Bot.addMessage(this,"Unknown Animation: " + animation);
				return random(2000, 3000);
				}
			}
		}

		return random(500, 1000);
	}

	public RSInterface MimeInterface() {
		return getInterface(188,0);
	}

	@Override
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getScriptCategory() {
		return "Random";
	}

	@Override
	public String getScriptDescription() {
		// TODO Auto-generated method stub
		return "Solves the mime random";
	}
}